﻿
using Xacml.Core;
using System;
using System.Xml;

namespace Xacml.Core.Policy
{
  public class RuleElementReadWrite : XacmlElement
  {
    private string _id = string.Empty;
    private string _description = string.Empty;
    private ConditionElementReadWrite _condition;
    private Effect _effect;
    private TargetElementReadWrite _target;

    public virtual string Id
    {
      get
      {
        return this._id;
      }
      set
      {
        this._id = value;
      }
    }

    public virtual string Description
    {
      get
      {
        return this._description;
      }
      set
      {
        this._description = value;
      }
    }

    public virtual ConditionElementReadWrite Condition
    {
      get
      {
        return this._condition;
      }
      set
      {
        this._condition = value;
      }
    }

    public bool HasCondition
    {
      get
      {
        return this._condition != null;
      }
    }

    public bool HasTarget
    {
      get
      {
        return this._target != null;
      }
    }

    public virtual TargetElementReadWrite Target
    {
      get
      {
        return this._target;
      }
      set
      {
        this._target = value;
      }
    }

    public virtual Effect Effect
    {
      get
      {
        return this._effect;
      }
      set
      {
        this._effect = value;
      }
    }

    public override bool IsReadOnly
    {
      get
      {
        return false;
      }
    }

    public RuleElementReadWrite(string id, string description, TargetElementReadWrite target, ConditionElementReadWrite condition, Effect effect, XacmlVersion schemaVersion)
      : base(XacmlSchema.Policy, schemaVersion)
    {
      this._id = id;
      this._description = description;
      this._target = target;
      this._condition = condition;
      this._effect = effect;
    }

    public RuleElementReadWrite(XmlReader reader, XacmlVersion schemaVersion)
      : base(XacmlSchema.Policy, schemaVersion)
    {
      if (reader.LocalName == "Rule" && this.ValidateSchema(reader, schemaVersion))
      {
        this._id = reader.GetAttribute("RuleId");
        this._effect = (Effect) Enum.Parse(typeof (Effect), reader.GetAttribute("Effect"), false);
        while (reader.Read())
        {
          switch (reader.LocalName)
          {
            case "Description":
              this._description = reader.ReadElementString();
              break;
            case "Target":
              this._target = new TargetElementReadWrite(reader, schemaVersion);
              break;
            case "Condition":
              this._condition = new ConditionElementReadWrite(reader, schemaVersion);
              break;
          }
          if (reader.LocalName == "Rule" && reader.NodeType == XmlNodeType.EndElement)
            break;
        }
      }
      else
        throw new Exception(Resource.ResourceManager[Resource.MessageKey.exc_invalid_node_name, new object[1]
        {
          (object) reader.LocalName
        }]);
    }
  }
}
